package com.haifeng.questionbankcrud;

import com.haifeng.questionbankcrud.dao.SingleChoiceDao;
import com.haifeng.questionbankcrud.dao.impl.SingleChoiceDaoImpl;
import com.haifeng.questionbankcrud.pojo.SingleChoice;
import com.haifeng.questionbankcrud.services.SingleChoiceService;
import com.haifeng.questionbankcrud.services.impl.SingleChoiceServiceImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class SingleChoiceController implements Initializable {
    SingleChoiceService scService = new SingleChoiceServiceImpl();

    private HelloApplication helloApplication;
    //插入界面信息框
    @FXML
    private TextArea SCquestion;
    @FXML
    private TextField SC_option_A;
    @FXML
    private TextField SC_option_B;
    @FXML
    private TextField SC_option_C;
    @FXML
    private TextField SC_option_D;
    @FXML
    private TextField SC_answer;
    @FXML
    private TextField SC_point;
    //插入界面提示框
    @FXML
    private Label insert_msg;
    //查询界面输入框
    @FXML
    private TextField Qpoint;
    //查询界面输出表格
    @FXML
    TableView<SingleChoice> table;
    //删除界面输出表格
    @FXML
    TableView<SingleChoice> D_table;
    //删除界面id输入框
    @FXML
    private TextField D_id;


    //输出界面提示框
    @FXML
    private Label D_msg;

    public void onDeleteButtonClicked() {



        String text = D_id.getText();
        Integer id = Integer.valueOf(text);
        SingleChoice singleChoice = new SingleChoice();
        singleChoice.setQ_id(id);
        int i = scService.deleteSingleChoice(singleChoice);
        D_msg.setText(i + "条记录被删除");

    }


    //查询全部按钮操作（点我查看题目）
    public void onQueryAllButtonClicked() {

        SingleChoiceDao dao = new SingleChoiceDaoImpl();
        List<SingleChoice> singleChoices = dao.selectAllChoice();
        //预封装列表
        ObservableList list = FXCollections.observableList(singleChoices);

        TableColumn tc1 = new TableColumn("题号");
        tc1.setCellValueFactory(new PropertyValueFactory<>("q_id"));
        tc1.setMinWidth(100);

        TableColumn tc2 = new TableColumn("题干");
        tc2.setCellValueFactory(new PropertyValueFactory<>("question"));
        tc2.setMinWidth(100);

        TableColumn tc3 = new TableColumn("选项A");
        tc3.setCellValueFactory(new PropertyValueFactory<>("option_a"));
        tc3.setMinWidth(100);

        TableColumn tc4 = new TableColumn("选项B");
        tc4.setCellValueFactory(new PropertyValueFactory<>("option_b"));
        tc4.setMinWidth(100);

        TableColumn tc5 = new TableColumn("选项C");
        tc5.setCellValueFactory(new PropertyValueFactory<>("option_c"));
        tc5.setMinWidth(100);

        TableColumn tc6 = new TableColumn("选项D");
        tc6.setCellValueFactory(new PropertyValueFactory<>("option_d"));
        tc6.setMinWidth(100);

        TableColumn tc7 = new TableColumn("答案");
        tc7.setCellValueFactory(new PropertyValueFactory<>("answer"));
        tc7.setMinWidth(100);

        TableColumn tc8 = new TableColumn("知识点");
        tc8.setCellValueFactory(new PropertyValueFactory<>("point"));
        tc8.setMinWidth(100);
        //写入表格
        D_table.getColumns().clear();
        D_table.getColumns().addAll(tc1, tc2, tc3, tc4, tc5, tc6, tc7, tc8);
        D_table.setItems(list);
    }

    //查询按钮操作
    public void onQueryButtonClicked() {
        System.out.println("query button clicked!");
        //封装对象
        String qpointText = Qpoint.getText();

        SingleChoice singleChoice = new SingleChoice();
        singleChoice.setPoint(qpointText);
        //调用查询
        List<SingleChoice> singleChoices = scService.selectSingleChoice(singleChoice);

        //预封装列表
        ObservableList list = FXCollections.observableList(singleChoices);
        //预处理表格

        TableColumn tc1 = new TableColumn("题号");
        tc1.setCellValueFactory(new PropertyValueFactory<>("q_id"));
        tc1.setMinWidth(100);

        TableColumn tc2 = new TableColumn("题干");
        tc2.setCellValueFactory(new PropertyValueFactory<>("question"));
        tc2.setMinWidth(100);

        TableColumn tc3 = new TableColumn("选项A");
        tc3.setCellValueFactory(new PropertyValueFactory<>("option_a"));
        tc3.setMinWidth(100);

        TableColumn tc4 = new TableColumn("选项B");
        tc4.setCellValueFactory(new PropertyValueFactory<>("option_b"));
        tc4.setMinWidth(100);

        TableColumn tc5 = new TableColumn("选项C");
        tc5.setCellValueFactory(new PropertyValueFactory<>("option_c"));
        tc5.setMinWidth(100);

        TableColumn tc6 = new TableColumn("选项D");
        tc6.setCellValueFactory(new PropertyValueFactory<>("option_d"));
        tc6.setMinWidth(100);

        TableColumn tc7 = new TableColumn("答案");
        tc7.setCellValueFactory(new PropertyValueFactory<>("answer"));
        tc7.setMinWidth(100);

        TableColumn tc8 = new TableColumn("知识点");
        tc8.setCellValueFactory(new PropertyValueFactory<>("point"));
        tc8.setMinWidth(100);

        table.getColumns().clear();
        table.getColumns().addAll(tc1, tc2, tc3, tc4, tc5, tc6, tc7, tc8);

        table.setItems(list);

    }


    //插入按钮操作
    public void onInsertButtonClicked() {
        //获取值封装对象
        String questionText = SCquestion.getText();
        String option_aText = SC_option_A.getText();
        String option_bText = SC_option_B.getText();
        String option_cText = SC_option_C.getText();
        String option_dText = SC_option_D.getText();
        String answerText = SC_answer.getText();
        String pointText = SC_point.getText();
        SingleChoice singleChoice = new SingleChoice();
        singleChoice.setQuestion(questionText);
        singleChoice.setOption_a(option_aText);
        singleChoice.setOption_b(option_bText);
        singleChoice.setOption_c(option_cText);
        singleChoice.setOption_d(option_dText);
        singleChoice.setAnswer(answerText);
        singleChoice.setPoint(pointText);
        System.out.println("controller@@" + singleChoice);
        int i = scService.insertSingleChoice(singleChoice);
        System.out.println("有" + i + "条题目被插入");
        insert_msg.setText("有" + i + "条题目被插入");
    }

    public void setHelloApplication(HelloApplication helloApplication) {
        this.helloApplication = helloApplication;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }
}
